// MainFrm.cpp : implementation file
//

#include "stdafx.h"
#include "TravianAutoTask.h"
#include "MainFrm.h"
#include "FormTask.h"
#include "ViewOutput.h"
#include "ViewTaskList.h"


// CMainFrm

IMPLEMENT_DYNCREATE(CMainFrm, CFrameWnd)

CMainFrm::CMainFrm(): m_bCreated(false)
{
}

CMainFrm::~CMainFrm()
{
}


BEGIN_MESSAGE_MAP(CMainFrm, CFrameWnd)
	ON_WM_CREATE()
	ON_UPDATE_COMMAND_UI(ID_FILE_OPEN,	&CMainFrm::OnUpdateFileOpen)
	ON_COMMAND(ID_FILE_OPEN,			&CMainFrm::OnFileOpen)
	ON_COMMAND(ID_FILE_FONT,			&CMainFrm::OnFileFont)
	ON_WM_CLOSE()
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};


// CMainFrm message handlers
int CMainFrm::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  Add your specialized creation code here
	SetIcon(::AfxGetApp()->LoadIcon(IDR_MAIN), TRUE);
	SetIcon(::AfxGetApp()->LoadIcon(IDR_MAIN), FALSE);


	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAIN))
	{
		TRACE0("Create tool bar failed\n");
		return -1;  
	}
	{	// CToolBar only can load 16 color bitmap, so here reload 256 bitmap file.
		CBitmap bmp;
		bmp.LoadBitmap(IDR_TOOLBAR256);
		m_wndToolBar.SetBitmap( (HBITMAP)(bmp.Detach()) );
	}

	if (!m_wndStatusBar.Create(this) || 
		!m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Create status bar failed\n");
		return -1;  
	}


	return 0;
}

BOOL CMainFrm::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
	// TODO: Add your specialized code here and/or call the base class

	// create a splitter with 2 row, 1 columns
	if (!m_wndSplitter.CreateStatic(this, 2, 1))
	{
		TRACE0("Failed to CreateStaticSplitter\n");
		return FALSE;
	}

	// add the second splitter pane - which is a nested splitter with 2 cols
	if (!m_wndSplitter2.CreateStatic(
		&m_wndSplitter,     // our parent window is the first splitter
		1, 2,               // the new splitter is 1 rows, 2 column
		WS_CHILD | WS_VISIBLE | WS_BORDER,  // style, WS_BORDER is needed
		m_wndSplitter.IdFromRowCol(0, 0)
			// new splitter is in the first row, 2nd column of first splitter
	   ))
	{
		TRACE0("Failed to create nested splitter\n");
		return FALSE;
	}

	// add the first splitter pane - the default view in column 1
	if (!m_wndSplitter.CreateView(1, 0,
		RUNTIME_CLASS(CViewOutput), CSize(lpcs->cx, ((lpcs->cy)>>1) ), pContext))
	{
		TRACE0("Failed to create first pane\n");
		return FALSE;
	}

	// now create the two views inside the nested splitter

	if (!m_wndSplitter2.CreateView(0, 0,
		RUNTIME_CLASS(CFormTask), CSize( ((lpcs->cx)>>1), ((lpcs->cy)>>1) ), pContext))
	{
		TRACE0("Failed to create second pane\n");
		return FALSE;
	}
	if (!m_wndSplitter2.CreateView(0, 1,
		RUNTIME_CLASS(CViewTaskList), CSize( ((lpcs->cx)>>1), ((lpcs->cy)>>1) ), pContext))
	{
		TRACE0("Failed to create third pane\n");
		return FALSE;
	}

	m_bCreated = true;
	if (m_bCreated)
	{
		CRect rect;
		GetWindowRect( &rect );
		m_wndSplitter.SetRowInfo(0, rect.Height()>>1, 10);
		m_wndSplitter2.SetColumnInfo(0, rect.Width()>>1, 10);
		m_wndSplitter.RecalcLayout();
		m_wndSplitter2.RecalcLayout();
	}

	return true;

	//return CFrameWnd::OnCreateClient(lpcs, pContext);
}


void CMainFrm::Init(Tasks* pts)
{
	((CViewOutput*)m_wndSplitter.GetPane(1, 0))->Init();
	((CFormTask*)m_wndSplitter2.GetPane(0, 0))->Init( (CViewTaskList*)(m_wndSplitter2.GetPane(0, 1)), pts );
}



void CMainFrm::OnUpdateFileOpen(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here

	// because this application has no document, so no message route to client area sub-view, we should dispatch manually.
	((CFormTask*)(m_wndSplitter2.GetPane(0, 0)))->OnUpdateFileOpen(pCmdUI);
}

void CMainFrm::OnFileOpen()
{
	// TODO: Add your command handler code here

	// because this application has no document, so no message route to client area sub-view, we should dispatch manually.
	m_wndSplitter2.GetPane(0, 0)->PostMessage(WM_COMMAND, ID_FILE_OPEN, NULL);
}

void CMainFrm::OnFileFont()
{
	// TODO: Add your command handler code here
	// because this application has no document, so no message route to client area sub-view, we should dispatch manually.
	m_wndSplitter.GetPane(1, 0)->PostMessage(WM_COMMAND, ID_FILE_FONT, NULL);
}

void CMainFrm::OnClose()
{
	// TODO: Add your message handler code here and/or call default

	((CFormTask*)m_wndSplitter2.GetPane(0, 0))->WaitScheduleThreadEnd();
	CFrameWnd::OnClose();
}



